{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "945b804d",
   "metadata": {},
   "source": [
    "--------------------\n",
    "* 项目日期：2021-12-23\n",
    "* week17\n",
    "* 项目人：马子娟\n",
    "* 项目名字全称&元音字母查询aap\n",
    "* 项目语言：Python+flask\n",
    "* 参考文档：\n",
    "> 1. [Flask文档](https://dormousehole.readthedocs.io/en/latest/quickstart.html#id2)\n",
    "> 2.\n",
    "--------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c17a382",
   "metadata": {},
   "source": [
    "* 项目的核心知识点：\n",
    "> 1. Flask角色定位：中央登记中心\n",
    ">> 1. 路由：\n",
    ">>> 1. url 相对路径（不完整的路径）：相对于http://127.0.0.1:5000 的相对路径\n",
    ">>> 2. HTTP方法：【GET 默认】【POST 有用户输入】\n",
    ">> 2. 视图函数：\n",
    ">>> 1.书写函数的过程(if elif else for while ect.) 处理数据\n",
    ">>> 2.return(返回值)：text文本、HTML文件、JSON数据文件 etc.\n",
    "> 2. HTML 模板渲染\n",
    ">> 1.Python文件--->HTML文件--->render_template()--->from flask import render_templater\n",
    ">> 2.在 期末项目 iypnb文件 同一个路径下 新建一个templates文件夹专门存放 页面文件\n",
    ">> 3. render_template('HTML 文件名')\n",
    "> 3.[from表单](https://www.w3school.com.cn/html/html_forms.asp)\n",
    ">> 1.用于收集用户输入\n",
    ">> 2.input：text 输入框；submit 提交按钮\n",
    ">> 3.一般是POST请求，需要注意以下两点：\n",
    ">>> 1.method='POST'\n",
    ">>> 2. action='应对的页面路由'\n",
    "\n",
    "\"\"\"\n",
    "<form method='POST'  action='应对的页面路由'>\n",
    " First name:<br>\n",
    "<input type=\"text\" name=\"firstname\">\n",
    "<br>\n",
    " Last name:<br>\n",
    "<input type=\"text\" name=\"lastname\">\n",
    "<br>\n",
    "<input type=\"submit\" name=\"提交\">\n",
    "</form> \n",
    "\"\"\"\n",
    "\n",
    "> 4. request\n",
    ">> 1. request.form 接受数据\n",
    ">> 2. HTML 输入框的变量名 ； python 变量名\n",
    ">> 3.request.form"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "476b9621",
   "metadata": {},
   "outputs": [],
   "source": [
    "#help(app.route)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d0a3eb28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app \"__main__\" (lazy loading)\n",
      " * Environment: production\n",
      "   WARNING: This is a development server. Do not use it in a production deployment.\n",
      "   Use a production WSGI server instead.\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      " * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n",
      "127.0.0.1 - - [30/Dec/2021 18:24:35] \"\u001b[37mGET / HTTP/1.1\u001b[0m\" 200 -\n",
      "127.0.0.1 - - [30/Dec/2021 18:24:35] \"\u001b[33mGET /docs/5.1/dist/js/bootstrap.bundle.min.js HTTP/1.1\u001b[0m\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 18:24:35] \"\u001b[33mGET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\u001b[0m\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 18:24:38] \"\u001b[37mGET /index HTTP/1.1\u001b[0m\" 200 -\n",
      "127.0.0.1 - - [30/Dec/2021 18:24:38] \"\u001b[33mGET /docs/5.1/dist/js/bootstrap.bundle.min.js HTTP/1.1\u001b[0m\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 18:24:38] \"\u001b[33mGET /docs/5.1/dist/js/bootstrap.bundle.min.js HTTP/1.1\u001b[0m\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 18:24:38] \"\u001b[33mGET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\u001b[0m\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 18:24:41] \"\u001b[33mGET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\u001b[0m\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 18:24:52] \"\u001b[33mGET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\u001b[0m\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 18:26:04] \"\u001b[37mGET /index HTTP/1.1\u001b[0m\" 200 -\n",
      "127.0.0.1 - - [30/Dec/2021 18:26:04] \"\u001b[33mGET /docs/5.1/dist/js/bootstrap.bundle.min.js HTTP/1.1\u001b[0m\" 404 -\n",
      "127.0.0.1 - - [30/Dec/2021 18:26:04] \"\u001b[33mGET /docs/5.1/assets/img/favicons/manifest.json HTTP/1.1\u001b[0m\" 404 -\n"
     ]
    }
   ],
   "source": [
    "from flask import Flask,request\n",
    "from flask import render_template\n",
    "\n",
    "app = Flask(__name__)\n",
    "\n",
    "# 1.【GET】\n",
    "@app.route(\"/\",methods = ['GET'])\n",
    "def hello_world():\n",
    "    return render_template('hello.html')\n",
    "\n",
    "\n",
    "@app.route(\"/index\",methods = ['GET'])\n",
    "def index():\n",
    "    return render_template('index.html')\n",
    "\n",
    "# 2.【POST】\n",
    "@app.route(\"/result\",methods = ['POST'])\n",
    "def result():\n",
    "    #总过程： HTML 获取用户数据---> python获取HTML数据--->处理数据--->传给HTML，做样式，提升用户视觉体验\n",
    "    #思考:form的数据提交过来，用request.form接收？\n",
    "    py_firstname = request.form['firstname']\n",
    "    py_lastname = request.form['lastname']\n",
    "    #对其数据进行处理操作\n",
    "    py_fullname = py_firstname.title() + '' + py_lastname.title()\n",
    "    #将 py_fullname 传给 指定的 HTML\n",
    "    return render_template('result.html',\n",
    "                          fullname = py_fullname)\n",
    "\n",
    "@app.route(\"/index_vowels\",methods = ['GET'])\n",
    "def index_vowels():\n",
    "    return render_template('index_vowels.html')\n",
    "\n",
    "\n",
    "@app.route(\"/result_vowels\",methods=['POST'])\n",
    "def result_vowels():\n",
    "\n",
    "    py_word = request.form['word']\n",
    "    py_vowels = request.form['vowels']\n",
    "    py_found = {}\n",
    "    for i in py_word:\n",
    "        if i in py_vowels:\n",
    "            py_found.setdefault(i,0)\n",
    "            py_found[i]+=1\n",
    "            \n",
    "    return render_template('result_vowels.html',\n",
    "                          found = py_found)\n",
    "\n",
    "\n",
    "if __name__== '__main__':\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dab81551",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
